<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <!-- <a-col :span="24">
            <a-form-item label="所属风场" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-depart v-decorator="['orgname']" multi/>
            </a-form-item>
          </a-col>
         
          <a-col :span="24">
            <a-form-item label="项目名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['productbelongvalue']" placeholder="请输入项目名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="物料名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['productnamevalue']" placeholder="请输入物料名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="物料型号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['producttypename']" placeholder="请输入物料型号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="物料厂家" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['factorynamevalue']" placeholder="请输入物料厂家"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="物料大类" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['codesubtypename']" placeholder="请输入物料大类"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="物料小类" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['codetypename']" placeholder="请输入物料小类"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="库存数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['surplusnum']" placeholder="请输入库存数量"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="告警数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['risksurplus']" placeholder="请输入告警数量"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="报废数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['scrapnum']" placeholder="请输入报废数量"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="回库可用" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['reposused']" placeholder="请输入回库可用"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="回库待处理" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['reposprocess']" placeholder="请输入回库待处理"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="单价" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['unitprice']" placeholder="请输入单价"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['unitvalue']" placeholder="请输入单位"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="是否本厂" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['ownorothers']" placeholder="请输入是否本厂"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="存放位置" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['productarea']" placeholder="请输入存放位置"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="设备用途" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['productencodings']" placeholder="请输入设备用途"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['remark']" placeholder="请输入备注"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="文件" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['filename']" placeholder="请输入文件"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="物料编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['productencoding']" placeholder="请输入物料编码"></a-input>
            </a-form-item>
          </a-col>
         -->
          <a-col :span="24">
            <a-form-item label="实际数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['actualNum']" placeholder="请输入实际数量"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="盘点备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['inventoryRemake']" placeholder="请输入盘点备注"></a-input>
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JSelectDepart from '@/components/jeecgbiz/JSelectDepart'
  import JSearchSelectTag from '@/components/dict/JSearchSelectTag'

  export default {
    name: 'DtKczlForm',
    components: {
      JFormContainer,
      JSelectDepart,
      JSearchSelectTag,
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/inventory_product/dtKczl/add",
          edit: "/inventory_product/dtKczl/edit",
          queryById: "/inventory_product/dtKczl/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'orgname','equiptypecode','equipsubtypecode','productname','factoryname','producttype','productbelong','productusing','surplusnum','risksurplus','scrapnum','reposused','reposprocess','unitprice','unit','ownorothers','productarea','productencodings','remark','filename','productencoding','unitvalue','productbelongvalue','productnamevalue','producttypename','factorynamevalue','codesubtypename','codetypename','actualNum','inventoryRemake'))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'orgname','equiptypecode','equipsubtypecode','productname','factoryname','producttype','productbelong','productusing','surplusnum','risksurplus','scrapnum','reposused','reposprocess','unitprice','unit','ownorothers','productarea','productencodings','remark','filename','productencoding','unitvalue','productbelongvalue','productnamevalue','producttypename','factorynamevalue','codesubtypename','codetypename','actualNum','inventoryRemake'))
      },
    }
  }
</script>